var xmlHttp=createNewXmlHttpRequest();
var currentChapterId='';

function createNewXmlHttpRequest(){
	var xmlHttp;
	try{
		xmlHttp=new XMLHttpRequest();
	}catch(e){}

	if(!xmlHttp){
		alert("Error while creating XMLHttpRequest object");
	}else{
		return xmlHttp;
	}
}

function callServer(){
	if(xmlHttp){
		
		//send new elements		
		try{
			if((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && friendlyComments.newElemQueue.length > 0){
				var newElemIndex=friendlyComments.newElemQueue.shift();
				//console.dir(newElemIndex.content);
	
				var cmtId=newElemIndex.attributes[0].firstChild.data;
				var author=newElemIndex.childNodes[0].firstChild.data;
				var elemId=newElemIndex.childNodes[1].firstChild.data;;
				var date=newElemIndex.childNodes[2].firstChild.data;;
				var content=newElemIndex.childNodes[3].firstChild.data;;
				//trimitere parametrii

				var url="process.php";
				var params="procXHRComments=1&xhrCurrChapId="+currentChapterId+"&xhrCommand=new&xhrCmtId="+cmtId+"&xhrAuthor="+author+"&xhrElemId="+elemId+"&xhrDate="+date+"&xhrContent="+content;
				xmlHttp.open("POST", url, true);

				//header settings
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", params.length);
				xmlHttp.setRequestHeader("Connection", "close");

				xmlHttp.onreadystatechange=handleRequestStateChange;
				xmlHttp.send(params);
				return true;
			}
		}catch(e){
			alert(e.toString());
		}
		
		//update old elements		
		try{
			if((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && friendlyComments.changesQueue.length > 0){
				//alert("inside");
				var newElemIndex=friendlyComments.changesQueue.shift();
				//console.dir(newElemIndex);
	
				var cmtId=newElemIndex.attributes[0].firstChild.data;
				var author=newElemIndex.childNodes[0].firstChild.data;
				var elemId=newElemIndex.childNodes[1].firstChild.data;;
				var date=newElemIndex.childNodes[2].firstChild.data;;
				var content=newElemIndex.childNodes[3].firstChild.data;;
				//trimitere parametrii

				var url="process.php";
				var params="procXHRComments=1&xhrCurrChapId="+currentChapterId+"&xhrCommand=update&xhrCmtId="+cmtId+"&xhrAuthor="+author+"&xhrElemId="+elemId+"&xhrDate="+date+"&xhrContent="+content;
				//alert(params);
				xmlHttp.open("POST", url, true);

				//header settings
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", params.length);
				xmlHttp.setRequestHeader("Connection", "close");
				xmlHttp.onreadystatechange=handleRequestStateChange;
				
				xmlHttp.send(params);
				return true;
			}
		}catch(e){
			alert(e.toString());
		}
	}
}

//tratarea modificarii starii cererii trimise la server
function handleRequestStateChange(){
	if(xmlHttp.readyState == '4'){
		if(xmlHttp.status == 200){
			//tratarea raspunsului de la server
			try{
				handleServerResponse();
			}catch(e){
				alert("Error reading the response: "+e.toString());
			}
		}else{
			alert("There was a problem retrieving the data from server: "+xmlHttp.statusText);
		}
	}
}

//tratarea raspunsului de la server
function handleServerResponse(){
	var serverResponse=xmlHttp.responseText.toString();
	var helder=document.getElementById('serverResponseHelder');
	helder.innerHTML=serverResponse;
	if(serverResponse == "OK"){
		progBar();
	}
	if(friendlyComments.newElemQueue.length == 0 && friendlyComments.changesQueue.length == 0){
		saveChapterChangesEnd(currentChapterId);
	}
	setTimeout("callServer();", 500);
}

function sendChangeToServer(chapterId){
	if(currentChapterId == ''){
		currentChapterId=chapterId;
	}
	callServer();
}